<?php

class Controller_Downloads extends Controller_Site {

    public function action_index() {
        $downloads = ORM::factory('downloads')->order_by('category_id')->find_all();
        $categories = ORM::factory('category')->order_by('id')->find_all();
        $this->content->set('categories', $categories);
        $this->content->set('downloads', $downloads);
    }

    public function action_upload() {
        Upload::$default_directory = 'uploads';
        if (isset($_FILES['file'])) {
            if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {               
                $new = ORM::factory('downloads');
                $new->file_name = $this->clearfilename($_FILES['file']['name']);
                Upload::save($_FILES['file'], $new->file_name);
                if($this->request->post('description')==''){
                    $new->description = $new->file_name;
                }
                else{
                    $new->description = $this->request->post('description');
                }
                $new->format = $_FILES['file']['type'];
                $new->size = $_FILES['file']['size'];
                $new->category_id = $this->request->post('choose_category');
                $new->uploaded = date('Y-m-d H:i:s');
                $new->save();
                
                $this->setFlash('notice', 'Sikeres feltöltés!');
                $this->request->redirect('admin/tasks');
            }
        }
    }
    
    public function action_newcategory(){
        if($this->request->method()=='POST'){
            try {
                $category = ORM::factory('category');
                $category->name=$this->request->post('cat_name');
                if($this->CheckCategoryExists($this->request->post('cat_name'))){
                    throw new Exception('Létező kategória!');
                }
                $category->save();
                $this->setFlash('notice', 'Sikeresen felvetted az új kategóriát!');
                $this->request->redirect('admin/tasks');
            } catch (Exception $e) {
                $this->setFlash('error', $e->getMessage());
                $this->request->redirect('admin/tasks');
            }
        }
    }
    
    public function CheckCategoryExists($cat_name){
        $cat = ORM::factory('category')->where('name', '=', $cat_name)->find();
        if($cat->loaded()){
            return true;
        }
        return false;
    }
    
    public function action_editcategory(){
        if($this->request->method()=='POST'){
            try {
                $id = $this->request->post('id');
                $category = ORM::factory('category', $id);
                $category->name=$this->request->post('name');
                if($this->CheckCategoryExists($this->request->post('cat_name'))){
                    throw new Exception('Létező kategória!');
                }
                $category->save();

                $this->setFlash('notice', 'Sikeresen módosítottad az új kategóriát!');
                $this->request->redirect('admin/tasks');
            } catch (Exception $e) {
                $this->content->set('e', $e);
            }
        }
    }
    
    public function action_deletecategory(){
            $id = $this->request->param('id');
            $category = ORM::factory('category', $id);
            $category->delete();
            $this->request->redirect('admin/tasks');
    }

    protected function clearfilename($name) {
        $mit = array('á', 'é', 'í', 'ö', 'ő', 'ó', 'ü', 'ű', 'ú', 'Ö', 'Ü', 'Ó', 'Ő', 'Ú', 'É', 'Á', 'Ű', '×', ' ');
        $mire = array('a', 'e', 'i', 'o', 'o', 'o', 'u', 'u', 'u', 'O', 'U', 'O', 'O', 'U', 'E', 'A', 'U', 'x', '_');
        $newstr = str_replace($mit, $mire, $name);
        return $newstr;
    }

    public function action_editfiles() {
        if($this->request->method()=='POST'){
            try {
                $id = $this->request->post('file_id');
                $file = ORM::factory('downloads', $id);
                $cat_id = ORM::factory('category')->where('name', '=', $this->request->post('file_cat'))->find();
                $file->description = $this->request->post('description');
                $file->category_id = $cat_id->id;
                $file->save();
                $this->setFlash('notice', 'Sikeresen módosítottad a fájlt!');
                $this->request->redirect('admin/tasks');
            } catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
    }
    
    public function action_deletefiles(){
        $id = $this->request->param('id');
        $file = ORM::factory('downloads', $id);
        unlink(realpath('uploads').DIRECTORY_SEPARATOR.$file->file_name);
        $file->delete();
        $this->setFlash('notice', 'Sikeresen törölted a fájlt!');
        $this->request->redirect('admin/tasks');
    }
}
?>
